package com.yanqu.road.web.controller.game;

import com.yanqu.road.logic.bussiness.player.UserBussiness;
import com.yanqu.road.logic.bussiness.recharge.RechargeBussiness;
import com.yanqu.road.logic.config.ServerConfig;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.recharge.RechargeOrder;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eOrderStatus;
import com.yanqu.road.web.controller.BaseHttpServlet;
import com.yanqu.road.web.mgr.MallMgr;
import com.yanqu.road.webservice.invoke.GameWebServiceInvoke;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

@WebServlet(urlPatterns = "/androidcharge")
public class TestAndroidRecharge extends BaseHttpServlet {

	private static Logger logger = LogManager.getLogger(TestAndroidRecharge.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(0 == ServerConfig.RECHARGE_DEBUG_MODE) {//充值测试
			sendMessage(response, "101");
			return;
		}
		String customInfo = URLDecoder.decode(request.getParameter("custominfo"), "utf-8");
		RechargeOrder chargeOrder = RechargeBussiness.getRechargeOrder(customInfo);
		UserInfo userInfo = UserBussiness.getUserInfoByUserId(chargeOrder.getUserId());
		/*if(!userInfo.isAdmin()){//不是管理员
			sendMessage(response, "101");
			return;
		}*/
		if (null != chargeOrder) {
			logger.warn("user is debug recharge start,userId:{},orderNo:{}", chargeOrder.getUserId(), chargeOrder.getOrderNo());
			if (!chargeOrder.getOrderStatus().equals(eOrderStatus.HasBeenCharged)) {
				//游戏内到账
				chargeOrder.setOrderStatus(eOrderStatus.ToBeCharged);
				chargeOrder.setPlatformOrderNo("testPlatformOrder");
				chargeOrder.setSdkOrderNo("testSdkOrder");
				MallInfo mallInfo = MallMgr.getMallInfoByMallId(chargeOrder.getMallId());
				if(null != mallInfo) {
					RechargeBussiness.updateRechargeOrder(chargeOrder);
					GameWebServiceInvoke.notifyRechargeSuccess(customInfo, 0);
					logger.warn("user is debug recharge success,userId:{},orderNo:{}", chargeOrder.getUserId(), chargeOrder.getOrderNo());
				}
			}
		} else {
			sendMessage(response, "101");
			return;
		}
		sendMessage(response, "1");
	}
}
